<?php

namespace Miniprogram\Controller;

use Think\Controller;

class IndexController extends CoreController {

    protected $m;
    protected $key = 'XBYBZ-MEPKU-RFLVA-BXEXT-BGGHH-A2BZ4';
    public function _initialize(){

        parent::_initialize();

    }

    //首页banner关于我们

    public function index(){

        $product=M('product')->select();

        $cartype=M('cartype')->select();

        foreach ($product as $k=>$v){

            $product[$k]['floor_cost']=json_decode($v['floor_cost']);

            $product[$k]['appraise']=M('appraise')->where(['status'=>1,'product_id'=>$v['id']])->field('id,avatar,grade,addtime,text,pics,driver_name,nickname')->limit(3)->select();

            // var_dump($product[$k]['appraise']);
            foreach ($product[$k]['appraise'] as $key=>$item){

                $product[$k]['appraise'][$key]['pics']=json_decode($product[$k]['appraise'][$key]['pics']);

            }

        }
        foreach ($cartype as $k=>$v){

            $cartype[$k]['introduce']=json_decode($v['introduce']);

            $cartype[$k]['tag']=explode('|',$v['tag']);

        }

        $config=M('config')->where(['key'=>['in',['Notice','Tel']]])->select();

        $config=array_column($config,'value','key');

        $data=[

            'product'=>$product,

            'cartype'=>$cartype,

            'notice'=>$config['Notice'],

            'tel'=>$config['Tel']

        ];

        success($data);

    }

    public function json(){

//        $data=[

//            ['title'=>'有电梯','price'=>"0"],

//            ['title'=>'无电梯1层','price'=>"0"],

//            ['title'=>'无电梯2层','price'=>"10"],

//            ['title'=>'无电梯三层','price'=>"20"],

//        ];

        $data=[

            '一辆不够再来一辆','一辆不够再来一辆','一辆不够再来一辆'

        ];



        echo json_encode($data);

    }


        //统一记录计算距离
    public function distance(){
        $data = I('post.');
        $url = "https://apis.map.qq.com/ws/direction/v1/driving/?from=".$data['start_lat'].','.$data['start_lng']."&to=".$data['end_lat'].','.$data['end_lng']."&&output=json&callback=cb&key=".$this->key;
        $res = json_decode(sendGet($url),true);
        if($res['status']==0){
            $this->ajaxReturn(['status'=>1,'mile'=>bcdiv($res['result']['routes'][0]['distance'],1000,2)]);exit;
        }else{
            $this->ajaxReturn(['status'=>2,'msg'=>'请求失败']);exit;
        }
    }
}